﻿using AutoMapper;
using MediatR;
using SmartTracking.API.Write.Application.Commands.Permission_command;
using SmartTracking.Domain.UserInfo;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.Permission_handler
{
    public class UpdatePermissionCommandHandler:IRequestHandler<UpdatePermissionCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<Permissions> permissionRep;
        private readonly IBaseRepository<RolePermissionsMiddle> rolePermissionRep;
        private readonly IMapper mapper;

        public UpdatePermissionCommandHandler(
            IBaseRepository<Permissions> permissionRep,
            IBaseRepository<RolePermissionsMiddle> rolePermissionRep,
            IMapper mapper)
        {
            this.permissionRep = permissionRep;
            this.rolePermissionRep = rolePermissionRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(UpdatePermissionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.更新成功;
            result.Message = ApiEnum.更新成功.ToString();

            Permissions permissions = mapper.Map<Permissions>(request);
            permissions.PermissionLevenl = 1;
            permissions.PermissionValue = "1";
            permissions.ParentId = 1;
            permissions.PermissionType = "1";
            var res = await permissionRep.UpdateAsync(permissions);

            result.data = res;
            return result;
        }
    }
}
